let server = require('express')();
let mysql = require('mysql');
const port = 8080;


server.all("*", function(req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200); //让options尝试请求快速结束
    else
        next();
})
let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'liebiao'
});

// 尝试链接
sql.connect();
// 列表页
server.get("/libiaolist", function(request, response) {
    sql.query('select * from libiaolist', function(error, data) {
        if (error) {
            console.log(error);
            response.end("error");
        } else {
            response.send(JSON.stringify(data))
        }
    })

})

server.get(`/zhuce`, (request, response) => {
    let username = request.query.username;
    let password = request.query.password;

    sql.query(`SELECT * FROM user WHERE username="${username}"`, (error, data) => {
        if (error) {
            console.log(error)
            response.end("error")
            return
        }
        if (data.length === 0) {
            sql.query(`INSERT INTO user (username,password) VALUES ("${username}","${password}")`, (error, result) => {
                if (error) {
                    console.log(error);
                    response.end("error");
                    return
                }
                response.end("success")
            })
        } else {
            response.end("exist")
        }
    })
})

server.get("/denglu", (request, response) => {
    let username = request.query.username;
    let password = request.query.password;

    sql.query(`SELECT * FROM user WHERE username="${username}"`, (error, data) => {
        if (data.length === 0) {
            response.end("error")
            return;
        }

        if (data[0].password === password) {
            response.end("success")
        } else {
            response.end("error")
        }
    })
})

// 详情页
let id = 0;
server.get("/click", function(request, response) {
    id = request.query.id;
    server.get("/getxiangqing", function(request, response) {
        sql.query(`SELECT * FROM getxiangqingmore WHERE id=${id}`, function(error, data) {
            if (error) {
                console.log(error)
                response.send("error")
                return;
            }
            response.send(JSON.stringify(data));
        })
    })
})

//购物车第一页
server.get("/getnum", function(request, response) {
    let title = request.query.title;
    let price = request.query.price;
    let num = request.query.num;
    let id = request.query.id;
    sql.query(`insert into yemianbiao (title,price,num,shopId) values ("${title}",${price},${num},${id})`, function(error, data) {
        if (error) {
            console.log(error);
            response.end("error");
        } else {
            response.send(JSON.stringify(data))
        }
    })

})

server.get("/clickdata", function(request, response) {
    id = request.query.id;
    server.get("/getdata", function(request, response) {
        sql.query(`select * from yemianbiao where shopId=${id}`, function(error, data) {
            if (error) {
                console.log(error);
                response.end("error");

            } else {
                response.send(JSON.stringify(data))
            }
        })
    })

    server.get("/getdatalist", function(request, response) {
        sql.query(`select * from dingdanmore where shopId=${id}`, function(error, data) {
            if (error) {
                console.log(error);
                response.end("error");

            } else {
                response.send(JSON.stringify(data))
            }
        })
    })
})

server.get("/editdata", function(request, response) {
    let name = request.query.name;
    let place = request.query.place;
    let placemore = request.query.placemore;
    let tel = request.query.tel;
    let id = request.query.id;
    sql.query(`insert into dingdanmore (name,place,placemore,tel,shopId) values ("${name}","${place}","${placemore}","${tel}",${id})`, function(error, data) {
        if (error) {
            console.log(error);
            response.end("error");
        } else {
            response.send(JSON.stringify(data))
        }
    })

})




server.listen(port);
console.log(`server is running at +${port}`)